home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Pointer Conversion
- Date: Sun, 21 Jan 96 19:16:16 GMT
- Organization: none
- Distribution: world
- Message-ID: <822251776snz@genesis.demon.co.uk>
- References: <4ds4jq$fo4@su3.in.net> <20JAN199622465412@erich.triumf.ca>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <20JAN199622465412@erich.triumf.ca>
- bennett@erich.triumf.ca "P.Bennett" writes:
-
- >In article <4ds4jq$fo4@su3.in.net>, poundss@in.net (Sam Pounds) writes...
- >>I am having a problem with a "string concatenate" function.
- >>When I compile my little program it works, but I get a
- >>"suspicious pointer" conversion warning. The function is
- >>below and I call it with two strings that I want to concatenate.
- >>
- >>char *my_strcat(const char *a, const char *b)
- >>{
- >> char done[1024];
- >> char *p = done;
- >>
- >> while (*a)
- >> *p++ = *a++;
- >> while (*b)
- >> *p++ = *b++;
- >> *p = '\0';
- >> return done; /* this is the suspicious pointer conversion error */
- >>}
- >
- >Your function is declared to return a char pointer, and you are returning an
- >array, which is not the same thing. Sometimes char array names act like char
- >pointers (particularly when used as function parameters), but not here.
-
- In this case done *does* evaluate to a pointer to the first element of the
- array. I don't know why the compiler would diagnose "suspicious pointer
- conversion" here since no pointer conversion is taking place.
-
- >A more important problem here is that "done" is an automatic array which will
- >cease to exist when the function returns, possibly being over-written on the
- >next call to another function.
-
- Yes, that is the obvious problem in the code. Maybe the compiler was trying
- to warn about this.
-
- >You could declare "done" as a char pointer, and malloc() some memory for it to
- >point to - this would solve both problems, but the calling function would have
- >to free() that malloc()ed memory sometime.
- >
- >You could also declare done as a static array (just add "static" before the
- >present declaration), then the array will exist for the life of the program.
- >I _think_ (and I know someone will correct me if I'm wrong) that changeing
- >"return done;" to "return &done;" will fix the "suspicious pointer conversion"
- >error.
-
- return &done would result in an illegal pointer conversion i.e. converting
- a char (*)[1024] to a char *.
-
- Usually the best approach in this sort of situation is to have the array
- allocated in the caller and pass pointer to it as an extra argument, instead
- of returning a pointer.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-